﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace FSS.UI
{
    public class EventTriggerListenerEx : EventTrigger
    {
        public delegate void PointEventCB(string name, int data, PointerEventData ped);
        public delegate void BaseEventCB(string name, int data, BaseEventData bed);
        public delegate void AxisEventCB(string name, int data, AxisEventData aed);
        
        public PointEventCB onBeginDrag;
        public BaseEventCB onCancel;
        public BaseEventCB onDeselect;
        public PointEventCB onDrag;
        public PointEventCB onDrop;
        public PointEventCB onEndDrag;
        public PointEventCB onInitializePotentialDrag;
        public AxisEventCB onMove;
        public PointEventCB onPointerClick;
        public PointEventCB onPointerDown;
        public PointEventCB onPointerEnter;
        public PointEventCB onPointerExit;
        public PointEventCB onPointerUp;
        public PointEventCB onScroll;
        public BaseEventCB onSelect;
        public BaseEventCB onSubmit;
        public BaseEventCB onUpdateSelected;
        bool ShowDebug = false;
        int UserData = 0;
        string UIName = "";
        static public EventTriggerListenerEx GetListener(Transform tr, int userData=0, bool setData=false)
        {
            EventTriggerListenerEx listener = tr.GetComponent<EventTriggerListenerEx>();
            if (listener == null)
                listener = tr.gameObject.AddComponent<EventTriggerListenerEx>();
            if(listener)
            {
                if(setData)
                {
                    listener.UserData = userData;
                }
                if (listener.UIName.Length == 0)
                {
                    listener.UIName = tr.name;
                }
            }
            return listener;
        }

        public override void OnBeginDrag(PointerEventData data)
        {
            if (onBeginDrag != null)
                onBeginDrag(UIName, UserData, data);
            if(ShowDebug)
                Debug.Log("OnBeginDrag [" + UIName + "] [" + UserData + "]");
        }

        public override void OnCancel(BaseEventData data)
        {
            if (ShowDebug)
                Debug.Log("OnCancel [" + UIName + "] [" + UserData + "]");
        }

        public override void OnDeselect(BaseEventData data)
        {
            if (onDeselect != null)
                onDeselect(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnDeselect [" + UIName + "] [" + UserData + "]");
        }

        public override void OnDrag(PointerEventData data)
        {
            if (onDrag != null)
                onDrag(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnDrag [" + UIName + "] [" + UserData + "]");
        }

        public override void OnDrop(PointerEventData data)
        {
            if (onDrop != null)
                onDrop(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnDrop [" + UIName + "] [" + UserData + "]");
        }

        public override void OnEndDrag(PointerEventData data)
        {
            if (onEndDrag != null)
                onEndDrag(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnEndDrag [" + UIName + "] [" + UserData + "]");
        }

        public override void OnInitializePotentialDrag(PointerEventData data)
        {
            if (onInitializePotentialDrag != null)
                onInitializePotentialDrag(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnInitializePotentialDrag [" + UIName + "] [" + UserData + "]");
        }

        public override void OnMove(AxisEventData data)
        {
            if (onMove != null)
                onMove(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnMove [" + UIName + "] [" + UserData + "]");
        }

        public override void OnPointerClick(PointerEventData data)
        {
            if (onPointerClick != null)
                onPointerClick(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnPointerClick [" + UIName + "] [" + UserData + "]");
        }

        public override void OnPointerDown(PointerEventData data)
        {
            if (onPointerDown != null)
                onPointerDown(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnPointerDown [" + UIName + "] [" + UserData + "]");
        }

        public override void OnPointerEnter(PointerEventData data)
        {
            if (onPointerEnter != null)
                onPointerEnter(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnPointerEnter [" + UIName + "] [" + UserData + "]");
        }

        public override void OnPointerExit(PointerEventData data)
        {
            if (onPointerExit != null)
                onPointerExit(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnPointerExit [" + UIName + "] [" + UserData + "]");
        }

        public override void OnPointerUp(PointerEventData data)
        {
            if (onPointerUp != null)
                onPointerUp(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnPointerUp [" + UIName + "] [" + UserData + "]");
        }

        public override void OnScroll(PointerEventData data)
        {
            if (onScroll != null)
                onScroll(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnScroll [" + UIName + "] [" + UserData + "]");
        }

        public override void OnSelect(BaseEventData data)
        {
            if (onSelect != null)
                onSelect(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnSelect [" + UIName + "] [" + UserData + "]");
        }

        public override void OnSubmit(BaseEventData data)
        {
            if (onSubmit != null)
                onSubmit(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnSubmit [" + UIName + "] [" + UserData + "]");
        }

        public override void OnUpdateSelected(BaseEventData data)
        {
            if (onUpdateSelected != null)
                onUpdateSelected(UIName, UserData, data);
            if (ShowDebug)
                Debug.Log("OnUpdateSelected [" + UIName + "] [" + UserData + "]");
        }
    }
}